package models

import (
	"encoding/json"
	"server-admin-api/views"
	"time"
)

type Customize struct {
	Id          int       `gorm:"primaryKey;autoIncrement" json:"id"` // 主键
	GroupId     int       `json:"group_id"`                           // 分组
	BasePath    string    `gorm:"size:255" json:"base_path"`          // 根路径
	Title       string    `gorm:"size:100" json:"title"`              // 服务名称
	Desc        string    `gorm:"size:255" json:"desc"`               // 服务描述
	Script      string    `json:"script"`                             // 脚本内容
	Status      string    `json:"status"`                             // 服务状态
	Linking     string    `json:"linking"`                            // 是否存在连接，连接地址
	LogPath     string    `json:"log_path"`                           // 日志路径
	ExecWay     string    `json:"exec_way"`                           // 执行方式
	UpTime      time.Time `json:"up_time"`
	AddTime     time.Time `json:"add_time"`
	RunTime     time.Time `json:"run_time"`
	RunLen      int64     `json:"run_len"`       // 运行时长
	LogType     string    `json:"log_type"`      // 日志类型
	LogReadLine int       `json:"log_read_line"` // 最大日志读取行数
}

func (cus Customize) MarshalJSON() ([]byte, error) {
	type temp Customize
	return json.Marshal(&struct {
		temp
		UpTime  DateTime `json:"up_time"`
		AddTime DateTime `json:"add_time"`
		RunTime DateTime `json:"run_time"`
	}{
		temp:    (temp)(cus),
		UpTime:  DateTime(cus.UpTime),
		AddTime: DateTime(cus.AddTime),
		RunTime: DateTime(cus.RunTime),
	})
}

func (cus Customize) NewBuild(param views.CustomizeCreate) Customize {
	return Customize{
		GroupId:  param.GroupId,
		BasePath: param.BasePath,
		Title:    param.Title,
		Desc:     param.Desc,
		Script:   param.Script,
		Status:   "waiting",
		Linking:  param.Linking,
		LogPath:  param.LogPath,
		ExecWay:  param.ExecWay,
		RunLen:   0,
		UpTime:   time.Now(),
		AddTime:  time.Now(),
		LogType: param.LogType,
		LogReadLine: param.LogReadLine,
	}
}
